package com.jike.noobmoney.mvp.view.activity;

import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.image.imagepicker.ImagePicker;
import com.image.imagepicker.bean.ImageItem;
import com.image.imagepicker.ui.ImageGridActivity;
import com.jike.noobmoney.R;
import com.jike.noobmoney.entity.BuzhouEntity;
import com.jike.noobmoney.util.Logger;
import com.jike.noobmoney.util.ToastUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class TestTwoActivity extends BaseActivity {


    @BindView(R.id.ll_view)
    LinearLayout llView;

    @Override
    protected int layoutResID() {
        return R.layout.activity_test_two;
    }

    @Override
    protected void initData() {

        View view1 = getView();
        addView(view1);
        addEvent(view1);



//        String u_id = "71";
//        String c_id = "1";
//        String picture = "";
//        String money = "10";
//        String number = "100";
//        String text = "关注公众号Firestorms";
//        String top = "1";
//        String opentype = "1";
//        String openurl = "15585355264841.jpg";
//        String step = "";
//
//
//        String[] names = {"1.打开微信", "2.搜索公众号Firestorms"};
//        String[] genders = {"15585355264841.jpg", "15585355264841.jpg"};
//        List<BuzhouEntity> students = new ArrayList<>();
//        for (int i = 0; i < names.length; i++) {
//            BuzhouEntity student = new BuzhouEntity();
//            student.setStep_text(names[i]);
//            student.setStep_pic(genders[i]);
//            students.add(student);
//        }
//        String jsonStr = new Gson().toJson(students);
//        Log.e("xuke",jsonStr);



    }

    View xkview;


    private void addEvent(View view) {

        TextView tvRemoveView = (TextView) view.findViewById(R.id.tv_remove_view);
        TextView tvAddView = (TextView) view.findViewById(R.id.tv_add_view);
        ImageView ivPic = (ImageView) view.findViewById(R.id.iv_pic);


        tvRemoveView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                removeView(view);
            }
        });

        tvAddView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtils.showShortToastSafe("添加布局");
                //tvAddView.setClickable(false);
                View view2 = getView();
                addView(view2);
                addEvent(view2);
            }
        });

        ivPic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ImagePicker.getInstance().setSelectLimit(1);
                ImagePicker.getInstance().setCrop(false);
                ImagePicker.getInstance().setShowCamera(true);
                ImagePicker.getInstance().setMultiMode(true);
                Intent intent1 = new Intent(context, ImageGridActivity.class);
                xkview = v;
                startActivityForResult(intent1, 100);
            }
        });
    }

    @SuppressWarnings("unchecked")
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == ImagePicker.RESULT_CODE_ITEMS) {
            //添加图片返回
            if (data != null && requestCode == 100) {
                ArrayList<ImageItem> images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                if (images != null) {
                    ImageView image = xkview.findViewById(R.id.iv_pic);
                    //image.setImageResource(R.mipmap.ic_launcher);
                    Logger.e("xuke", "--->" + images.get(0));

                    //if (!images.get(0).getPath().equals(image.getTag())) {
                        image.setTag(null);
                        Glide.with(this).load(new File(images.get(0).getPath())).into(image);
                        image.setTag(images.get(0).getPath());
                    //}

                }
            }
        }
    }


    private View getView() {
        LayoutInflater inflater = LayoutInflater.from(this);
        return inflater.inflate(R.layout.item_add_layout, null);
    }

    private void addView(View layout) {
        llView.addView(layout);
    }

    private void removeView(View layout) {
        llView.removeView(layout);
    }


    private void printText() {
        for (int i = 0; i < llView.getChildCount(); i++) {
            View view = llView.getChildAt(i);
            EditText etText = (EditText) view.findViewById(R.id.et_text);
            Logger.e("xuke", "==" + etText.getText());

            ImageView ivPic = (ImageView) view.findViewById(R.id.iv_pic);
            Logger.e("xuke", "iv==" + ivPic.getTag());

        }

    }


    @OnClick(R.id.tv_print)
    public void onViewClicked() {
        printText();
    }
}
